package love.yuanmengrobot.qdkaoqin.business;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.NetworkOnMainThreadException;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import love.yuanmengrobot.qdkaoqin.R;
import love.yuanmengrobot.qdkaoqin.entity.Clazz;
import love.yuanmengrobot.qdkaoqin.util.TeacherConstants;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class ToCreateClazzActivity extends AppCompatActivity {

    private Button createBu;
    private Button cancelBu;
    private EditText clazzNameEditText;
    private String Memberdi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_createclass);
        Intent intent = getIntent();
        Memberdi = intent.getStringExtra(TeacherConstants.MEMBERID);
        intiView();
    }

    public void intiView() {
        createBu = findViewById(R.id.bu_start_create);
        cancelBu = findViewById(R.id.bu_cancel_create);
        clazzNameEditText=findViewById(R.id.edit_clazz_name);
        createBu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              String name=  clazzNameEditText.getText().toString();
                post(Memberdi,name);
            }
        });
        cancelBu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private okhttp3.Callback callback = new okhttp3.Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
//            Log.e(TAG, "Failed to connect server!");
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response response)
                throws IOException {
            if (response.isSuccessful()) {
                final String body = response.body().string();
                System.out.println("67777777776:" + body);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(body.contains("成功")){
                            final AlertDialog alertDialog1 = new AlertDialog.Builder(ToCreateClazzActivity.this)
                                    .setTitle("提示")//标题
                                    .setMessage("创建成功！")//内容
                                    .setIcon(R.drawable.ic_baseline_check_24)
                                    .create();
                            alertDialog1.show();

//                            finish();
                        }else{
                            final AlertDialog alertDialog1 = new AlertDialog.Builder(ToCreateClazzActivity.this)
                                    .setTitle("提示")//标题
                                    .setMessage("创建失败！")//内容
                                    .setIcon(R.mipmap.ic_warning)
                                    .create();
                            alertDialog1.show();
                        }
//                        Gson gson = new Gson();
//                        Type jsonType =////////****
//                                new TypeToken<List<Clazz>>() {
//                                }.getType();
//                        List<Clazz> clazzListResponse =
//                                gson.fromJson(body, jsonType);
//                        for (Clazz cl : clazzListResponse) {
//                            clazzAdapter.add(cl);
//                        }
//                        clazzAdapter.notifyDataSetChanged();
                    }
                });
            } else {
            }
        }
    };

    private void post(String memberid,String clazzname) {
        FormBody.Builder builder = new FormBody.Builder();
        /* 添加两个参数 */
        builder.add("memberid", memberid)
                .add("classname", clazzname)
                .add("op", "add");
        FormBody body = builder.build();
        Request request = new Request.Builder().url("http://www.yuanmengrobot.love/attendance/clazzservlet").post(body).build();

        /* 下边和get一样了 */
        try {
            OkHttpClient client = new OkHttpClient();
            client.newCall(request).enqueue(callback);
        } catch (NetworkOnMainThreadException ex) {
            ex.printStackTrace();
        }
    }

}
